package com.linxianqin.springbootVue.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.convert.SysDictDataConvert;
import com.linxianqin.springbootVue.domain.convert.SysPostConvert;
import com.linxianqin.springbootVue.domain.dto.SysDictDataDTO;
import com.linxianqin.springbootVue.domain.dto.SysPostDTO;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.entity.SysDictData;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysDictDataQuery;
import com.linxianqin.springbootVue.domain.query.SysPostQuery;
import com.linxianqin.springbootVue.domain.vo.SysDictDataExcel;
import com.linxianqin.springbootVue.domain.vo.SysPostExcel;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysDictDataService;
import com.linxianqin.springbootVue.utils.poi.ExcelUtil;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



/**
 * @author lxq
 * @version 1.0
 * @description: 数据字典信息
 * @date 2020/12/15 11:30
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{

    private final SysDictDataService dictDataService;

    public SysDictDataController(SysDictDataService dictDataService){
        this.dictDataService=dictDataService;
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        log.info("---------根据字典类型查询字典数据信息-----------");
        List<SysDictData> data = dictDataService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictDataQuery query)
    {
        //构建分页bean
        Page<SysDictDataDTO> page = buildPage();
        //查询分页数据
        Page<SysDictDataDTO> dtoPage = dictDataService.selectSysDictDataPage(page, query);
        return getDataTable(dtoPage);
    }

//    @SysLog(title = "字典数据", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:dict:export')")
//    @GetMapping("/export")
//    public AjaxResult export(SysDictDataDTO dictDataDTO)
//    {
//        List<SysDictData> dataList = dictDataService.list();
//        //构建Excel工具包
//        ExcelUtil<SysDictDataExcel> util = new ExcelUtil<>(SysDictDataExcel.class);
//        //转化实体
//        List<SysDictDataExcel> excelList = SysDictDataConvert.INSTANCE.entity2Excel(dataList);
//        //序号id
//        for (int i = 0; i < excelList.size(); i++) {
//            excelList.get(i).setIndex(i+1);
//        }
//        return util.exportExcel(excelList, "字典数据");
//    }
}
